// pages/withdraw/withdraw.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        allWithdraw: 97,
        value: '',
        name: '',
        bank: '',
        bankNumber: '',
        phoneNumber: '',
        array: ['农业银行', '工商银行', '交通银行', '建设银行'],
        objectArray: [
            {
                id: 0,
                name: '农业银行'
            },
            {
                id: 1,
                name: '商业银行'
            },
            {
                id: 2,
                name: '交通银行'
            },
            {
                id: 3,
                name: '建设银行'
            }
        ],
        index: 0,
    },
    input1: function (e) {
        var that = this;
        this.setData({
            value: e.detail.value,
        })
    },
    input2: function (e) {
        var that = this;
        this.setData({
            name: e.detail.value,
        })
    },
    input3: function (e) {
        var that = this;
        console.log(e.detail.value)
        var value = that.data.array[e.detail.value];
        this.setData({
            bank: value,
        })
    },
    input4: function (e) {
        var that = this;
        this.setData({
            bankNumber: e.detail.value,
        })
    },
    input5: function (e) {
        var that = this;
        this.setData({
            phoneNumber: e.detail.value,
        })
    },
    withdrawMoney: function () {
        var that = this;
        
      if (!that.data.value || that.data.value < 1) {
            wx.showToast({
                title: '金额不小于1',
                image: '/img/fail.png',
                duration: 1500
            });
        } else if (parseFloat(that.data.value) > that.data.allWithdraw) {
            wx.showToast({
                title: '超出可提现金额',
                image: '/img/fail.png',
                duration: 1500
            });
        } else if (!that.data.name) {
            wx.showToast({
                title: '请输入姓名',
                image: '/img/fail.png',
                duration: 1500
            });
        } else if (!that.data.bank) {
            wx.showToast({
                title: '请输入开户行',
                image: '/img/fail.png',
                duration: 1500
            });
        } else if (!that.data.bankNumber) {
            wx.showToast({
                title: '请输入银行卡号',
                image: '/img/fail.png',
                duration: 1500
            });
        } else if (!that.data.phoneNumber) {
            wx.showToast({
                title: '请输入手机号',
                image: '/img/fail.png',
                duration: 1500
            });
        } else {
        wx.request({
          url: app.siteInfo.shoppath + 'checktixian',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid,
            com_part_id: app.siteInfo.com_part_id
          },
          success: function (res) {
            if(res.data.code == 200){
              wx.showToast({
                title: '已有提现申请',
                image: '/img/fail.png',
                duration: 1500
              });
            }else{
              wx.request({
                url: app.siteInfo.shoppath + 'savetixian',
                method: "post",
                data: {
                  uid: app.siteInfo.uid,
                  pid: app.siteInfo.pid,
                  com_part_id: app.siteInfo.com_part_id,
                  money: that.data.value,
                  name: that.data.name,
                  bank: that.data.bank,
                  bankNumber: that.data.bankNumber,
                  phoneNumber: that.data.phoneNumber
                },
                success: function (res) {
                  if (res.data.code == 200) {
                    wx.showToast({
                      title: '申请已提交',
                      icon: 'success',
                      duration: 1500
                    });
                    setTimeout(function(){
                      wx.navigateTo({
                        url: '/pages/mine/mine',
                      })
                    },2000)
                  } else {
                    wx.showToast({
                      title: '提交失败',
                      icon: 'fail',
                      duration: 1500
                    });
                  }

                }
              })
            }

          }
        })
          
            
        }

    },
  // 获取formid
  getFormId: function (e) {
    var formid = e.detail.formId;
    console.log(formid);
    wx.request({
      url: app.siteInfo.siteroot + 'saveformid',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        formid: formid
      },
      success: function (res) {

      }
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        function toDecimal2(x) {//强制保留2位小数
            var f = parseFloat(x);
            if (isNaN(f)) {
                return false;
            }
            var f = Math.round(x * 100) / 100;
            var s = f.toString();
            var rs = s.indexOf('.');
            if (rs < 0) {
                rs = s.length;
                s += '.';
            }
            while (s.length <= rs + 2) {
                s += '0';
            }
            return s;
        }
        // console.log(toDecimal2('3.12545'), toDecimal2(3))
        var that = this;
        wx.request({
          url: app.siteInfo.shoppath + 'mecenter',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid,
            com_part_id: app.siteInfo.com_part_id,
          },
          success: function (res) {
            var allWithdraw = toDecimal2(res.data.supercommission);
            that.setData({
              allWithdraw: allWithdraw,
              withdrawStandard:res.data.withdrawStandard
            })
          }
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})